<?php
$filename = "contacts-". date('mdy') . ".csv";
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
#$a = new AddressBookAction();
#$list = $a->getlist('', '', 1, "DESC");
#$list = json_decode($list);
#$list = $list[0];

$sm = new AddressBookModel('AddressBookAction');
$list = $sm->export();
$fp = fopen('php://output', 'w');
// check lib/AddressBookModel.class.php
// to properly import contacts;
$header = array(
              "Student No" => "stud_no",
              "Level" => "level",
              "FirstName" => 'firstname',
              "LastName" => 'lastname',
              "Middle Name" => 'middlename',
              "NickName" => "nick",
              "Email Add" => "email_add",
              "Address No" => "add_no",
              "Street" => "add_street",
              "BRGY" => "add_subdbrgy",
              "Town" => "add_townctry",
              "ZIP" => "add_zipcode",
              "Homenumber" => "homenumber",
              "Type" => "type",
              "Subcription status" => "subcription_status",
              "Number" => 'msisdn',
              "Group" => 'name'
          );
fputcsv($fp, array_keys($header));
foreach ($list as $l) {
    $line = array();
    foreach ($header as $key) {
        $line[] = $l[$key];
    }
    fputcsv($fp, $line);
}
fclose($fp);
